package net.aiion.weave.impl.bytecode;

import java.util.ArrayList;
import java.util.List;

import net.aiion.weave.spi.bytecode.BytecodeSink;
import net.aiion.weave.spi.bytecode.BytecodeSource;
import net.aiion.weave.spi.bytecode.events.BytecodeEvent;

public abstract class AbstractBytecodeSource implements BytecodeSource
{
    private final List<BytecodeSink> bytecodeSinks;

    private BytecodeContextImpl bytecodeContext;

    public AbstractBytecodeSource() {
        bytecodeSinks = new ArrayList<>();
        bytecodeContext = new BytecodeContextImpl(this);
    }

    @Override
    public void setProperty(final String aName, final Object aValue) {
        bytecodeContext.setProperty(aName, aValue);
    }

    @Override
    public void removeProperty(final String aName) {
        bytecodeContext.removeProperty(aName);
    }

    @Override
    public Object getProperty(final String aName) {
        return bytecodeContext.getProperty(aName);
    }

    @Override
    public <T> T getProperty(final String aName, final Class<T> aType) {
        return bytecodeContext.getProperty(aName, aType);
    }

    @Override
    public void addSink(final BytecodeSink aBytecodeSink) {
        bytecodeSinks.add(aBytecodeSink);
    }

    public void dispatchBytecodeEvent(final BytecodeEvent aBytecodeEvent) {
        bytecodeContext.setBytecodeSinks(bytecodeSinks);
        bytecodeContext.forwardBytecodeEvent(aBytecodeEvent);
    }
}
